from flask import make_response, jsonify

INVALID_FIELD_NAME_SENT_422 = {
    "http_code": 422,
    "code": "invalidField",
    "message": "Invalid fields found"
}

INVALID_INPUT_422 = {
    "http_code": 422,
    "code": "invalidInput",
    "message": "Invalid input"
}

MISSING_PARAMETERS_422 = {
    "http_code": 422,
    "code": "missingParameter",
    "message": "Missing parameters."
}

BAD_REQUEST_400 = {
    "http_code": 400,
    "code": "badRequest",
    "message": "Bad request"
}

SERVER_ERROR_500 = {
    "http_code": 500,
    "code": "serverError",
    "message": "Server error"
}

SERVER_ERROR_404 = {
    "http_code": 404,
    "code": "notFound",
    "message": "Resource not found"
}

FORBIDDEN_403 = {
    "http_code": 403,
    "code": "notAuthorized",
    "message": "You are not authorised to execute this."
}
UNAUTHORIZED_401 = {
    "http_code": 401,
    "code": "notAuthorized",
    "message": "Invalid authentication."
}

NOT_FOUND_HANDLER_404 = {
    "http_code": 404,
    "code": "notFound",
    "message": "route not found"
}

SUCCESS_200 = {
    'http_code': 200,
    'code': 'success'
}

SUCCESS_201 = {
    'http_code': 201,
    'code': 'success'
}

SUCCESS_204 = {
    'http_code': 204,
    'code': 'success'
}


# 生成响应的函数
def response_with(response, value=None, message=None, error=None, headers=None, pagination=None):
    result = {}
    # 合并 value 到结果字典
    if value:
        result.update(value)

    # 合并 message 到结果字典
    if response.get('message'):
        result.update({'message': response['message']})

    # 合并 code 到结果字典
    result['code'] = response['code']

    # 合并 error 到结果字典
    if error:
        result['errors'] = error

    # 合并 pagination 到结果字典
    if pagination:
        result['pagination'] = pagination

    # 初始化 headers
    headers = headers or {}
    # 添加默认的跨域请求头和服务器信息
    headers.update({
        'Access-Control-Allow-Origin': '*',
        'server': 'Flask REST API'
    })

    # 生成最终的响应
    return make_response(jsonify(result), response['http_code'], headers)
